<?php

namespace App\Http\Controllers\Common;

use App\Models\Order\Order;
use App\Models\Rbac\GuardUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

abstract class GuardController extends CommonController {

    public function getGuardUser(Request $request) {
        $token = $request->header('token');
        $id = checkJWT($token)['id'];
        $record = (new GuardUser())->getGuardUserById($id);
        $record->has_permission = $record->hasPermissions();
        return responseSuccess($record->toArray());
    }

    protected function getRecordByIds($model,string $ids) {
        $ids = array_filter(explode(',',$ids));
        $records = $model::whereIn('id',$ids)->get();
        return responseSuccess($records->toArray());
    }

    protected function changeCol($model,array $keyNames,array $keyValues,array $colNames,array $colValues) {
        foreach ($keyNames as $k=>$keyName) {
            $model::where($keyName,$keyValues[$k])->update([$colNames[$k]=>$colValues[$k]]);
        }
        return responseSuccess();
    }

    protected function delRecords($model,string $ids) {
        $ids = array_filter(explode(',',$ids));
        $model::whereIn('id',$ids)->delete();
        return responseSuccess();
    }


    public function download(Request $request) {
        $id = $request->input('id',0);
        $type = $request->input('type');

        $record = Order::where(compact('id'))->first();

        $path  = "";
        $name = "";
        if($type=='dingsun'){
            $path = $record->ding_sun_url;
        }elseif ($type=='cancelcase'){
            $path = $record->cancel_case_url;
        } elseif ($type=='invoice'){
            $path = $record->invoice_url;
        } elseif ($type=='qianzi'){
            $path = $record->qianzi_url;
        }elseif ($type=='repairList'){
            $path = $record->repair_list_img_url;
        } elseif ($type=='chengbao') {
            $path = '/chengbao.xlsx';
            $name='模板_承保单.xlsx';
        } elseif ($type=='lipei'){
            $path = '/lipei.xlsx';
            $name='模板_理赔单.xlsx';
        } else{
            $path = '/lipei.xlsx';
        }
        if(!empty($name)){
            return response()->download(public_path($path),$name);
        }
        return response()->download(public_path($path));


    }




}
